<template>
	<view>
		<view class="header">
			<button size="mini" class="change_btn" @click="cancelSet">取 消</button>
			<button size="mini" class="change_btn" @click='submitForm'>确 定</button>
		</view>
		<uni-forms ref="form" :modelValue="form" class="paddingAll" :rules="rules" labelAlign="right" label-width="180rpx">
			<uni-forms-item label="流程管理员" name="manageIds">
				<custom-tree-select mutiple dataLabel="label" :choseParent="false" :childernDisabled="true" placeholder="请选择模板管理员" :listData="deptOptions" v-model="defaultManage" />
			</uni-forms-item>
			<uni-forms-item label="流程类型" name="contractSignFileType">
				<!-- 0标准 1普通 -->
				<uni-data-select v-model="form.contractSignFileType" :localdata="selectFileArr" :clear="false" placeholder="请选择流程类型" />
			</uni-forms-item>
			<uni-forms-item label="流程名称" name="name">
				<uni-easyinput v-model="form.name" type="text" placeholder="请输入流程名称" />
			</uni-forms-item>
			<uni-forms-item label="合同类型" name="contractTypeId">
				<uni-data-select v-model="form.contractTypeId" :localdata="contractArr" :clear="false" @change="selectContractType"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="流程备注" name="remark" style="margin-bottom: 14rpx;">
				<uni-easyinput v-model="form.remark" type="textarea" placeholder="点击添加备注,最多300字!" />
			</uni-forms-item>	
			<uni-forms-item label="流程状态" style="margin-bottom: 0rpx;">
				<uni-data-checkbox v-model="form.status" :localdata="processStatus" class="status_checkbox" style="padding-top: 14rpx;"></uni-data-checkbox>
			</uni-forms-item>
			
		<!-- 发起人（签署业务） -->
		<div class="bg-page">
		  <div class="header_page">
			签约发起人<span style="font-size:24rpx;">（可指定单个、多个员工或整个部门作为发起人）</span></div>
			<custom-tree-select mutiple dataLabel="label" placeholder="请选择签约发起人" :listData="deptOptions" v-model="starterArr" @selectChange="selectDep" style="background-color: #fff;"/>
		</div>
		
		<!-- 本地文件签约文件及签署要求 -->
		<div class="bg-page" v-if="form.contractSignFileType==1">
		  <div class="header_page">签约文件及签署要求</div>
		  <div style="padding:6px 0;background: #fff;">
		      <uni-forms-item style="margin:10rpx 0">
				  <span slot="label" class="custom_label">合同文件</span>
				    <uni-easyinput style="width: 98%;" disabled type="text" v-model="placeholderData" />
		      </uni-forms-item>
		    <uni-forms-item prop="seconde_form" style="margin-bottom:10rpx">
				<span slot="label" class="custom_label">签署要求</span>
		        <!-- 二级表单 -->
		        <uni-forms size="small" labelWidth="170rpx" labelPosition="top" class="check_form">
		          <uni-forms-item style="margin-bottom:10rpx">
					  <span slot="label" class="custom_label">企业发起方</span>
					  <uni-data-checkbox multiple v-model="form.isCompanySign" :localdata="companySignArr" class="status_sign" @change='handlerCompanySignType'></uni-data-checkbox>
		          </uni-forms-item>
		          <uni-forms-item style="margin-bottom:10rpx">
					  <span slot="label" class="custom_label">企业接收方</span>
					  <uni-data-checkbox multiple v-model="form.isCompanySignReceive" :localdata="companySignArr" class="status_sign"></uni-data-checkbox>
		          </uni-forms-item> 
		          <uni-forms-item prop="isPersonSign" style="margin-bottom: 10rpx">
					  <span slot="label" class="custom_label">个人接收方</span>
					  <uni-data-checkbox multiple v-model="form.isPersonSign" :localdata="personalSignArr" class="status_sign" :min="1"></uni-data-checkbox>
		          </uni-forms-item>
		        </uni-forms>
		    </uni-forms-item>
		  </div>
		</div>
					
		<!-- 在线模板签约文件及签署要求 -->
		<div class="bg-page" v-if="form.contractSignFileType==0">
			<div class="header_page flex_between">
			    <span>签约文件及签署要求</span>
			    <button size="mini" style="float:right;" class="white_btn" @click="addContractTemplate">添加合同模板</button>
			</div>
		<div style="padding:12rpx 0;background: #fff;">
		<uni-forms-item prop="contractTypeId" style="margin:10rpx 0;">
			<span slot="label" class="custom_label">合同模板</span>
		    <div v-for="(item,index) in contractTemplateList" :key="index" :class="!!item.id && contractTemplateList.length>1?'nomarginBott':'marginBott'">
		      <uni-easyinput style="width: 98%;" disabled placeholder="请绑定在线合同模板" type="text" v-model="item.name" suffixIcon="close" @iconClick="deleteContractTemplate(item)"/>
			  <button class="green_btn" @click="openSelectTemplate(form,item)" size="mini" style="margin-top:20rpx">绑定</button>
			  <uni-data-checkbox :localdata="signRequire" style="padding-top: 14rpx;" v-if="!!item.id && contractTemplateList.length>1" v-model="item.isUseSignSet"></uni-data-checkbox>
		    </div>
		  </uni-forms-item>
		<uni-forms-item style="margin-bottom:10rpx;">
			<span slot="label" class="custom_label">签署要求</span>
		    <!-- 二级表单 -->
		      <uni-forms size="small" labelWidth="170rpx" labelPosition="top" class="check_form">
		        <uni-forms-item label="企业发起方：" prop="contractTemplateIds" style="margin-bottom:0">
		          <span v-if="currentRow.signSet==null">使用标准合同模板设置的签署要求</span>
		          <div v-else>
					  <uni-data-checkbox multiple v-model="currentRow.signSet" disabled :localdata="companySignArr" class="status_sign"></uni-data-checkbox>
		        </div>
		        </uni-forms-item>
		        <uni-forms-item label="企业接收方：" prop="contractTemplateIds" style="margin-bottom:0">
		          <span v-if="currentRow.signSet==null">使用标准合同模板设置的签署要求</span>
		          <span v-else>
					<uni-data-checkbox multiple v-model="currentRow.signSet" disabled :localdata="companySignArr" class="status_sign"></uni-data-checkbox>
		        </span>
		        </uni-forms-item>
		        <uni-forms-item label="个人接收方：" prop="contractTemplateIds" style="margin-bottom:0">
		          <span v-if="!currentRow.signPerson">使用标准合同模板设置的签署要求</span>
		          <span v-else>
					<uni-data-checkbox multiple v-model="currentRow.signSet" disabled :localdata="personalSignArr" class="status_sign" :min="1"></uni-data-checkbox>
		        </span>
		        </uni-forms-item>
		      </uni-forms>
		    </uni-forms-item>
			</div>
			</div>
			<!-- 签署人及签署方式 -->
			<div class="bg-page">
			 <div class="header_page" style="display: flex;align-items: center;justify-content: space-between;">
				<span>发起方签署人及签署方式</span>
				<button size="mini" type="success" class="white_btn" @click="addcard">添加签署人</button>
			</div>
			    <div style="padding:6px 0;background: #fff;">
			    <!-- 只显示签署人数据 -->
			    <uni-forms-item prop="firstSignPerson" style="margin:12rpx 0">
				  <span slot="label" class="custom_label">签署人</span>
					  <uni-data-select class="headerTwo_content" v-model="firstSignPerson.id" :localdata="staffList" :clear="false" placeholder="请选择签署人" @change="selectFirstGet" />
			    </uni-forms-item>
			    <uni-forms-item prop="ecSealCompanyId" style="margin-bottom:12rpx;">    
				  <span slot="label" class="custom_label">盖章方式</span>
				  <uni-data-select class="headerTwo_content" v-model="firstSignPerson.ecSealCompanyId" :localdata="sealCompanyList" :clear="false" placeholder="请选择盖章方式" @change="handlerEcSealCompany(firstSignPerson)" />
			    </uni-forms-item>
			    <!-- 普通合同签字方式 -->
			    <uni-forms-item prop="isCompanySign" v-if="form.contractSignFileType==1" style="margin-bottom:12px;">
					<span slot="label" class="custom_label">签字方式</span>
					<uni-data-select class="headerTwo_content" v-model="firstSignPerson.companySignType" :disabled="firstSignPerson.companySignType == null" :localdata="signTypeList" :clear="false" placeholder="无需盖签字章" />
			    </uni-forms-item>
			    <!-- 在线模板签字方式 -->
			    <uni-forms-item label="签字方式" prop="isCompanySign" v-if="form.contractSignFileType==0" style="margin-bottom:12px;">
					<span slot="label" class="custom_label">签字方式</span>
					<uni-data-select class="headerTwo_content" v-model="firstSignPerson.companySignType" :disabled="firstSignPerson.companySignType == null" :localdata="signTypeList" :clear="false" :placeholder="contractTemplatePlaceholder" />
			    </uni-forms-item>
				
				<!-- -----------------------------------  分割线  -------------------------------------------------- -->
				  <div v-for="(option, index) in selectSealData" :key="index">
			      <div style="border-top:1px dashed #ccc;padding-top: 40rpx;margin: 0 20rpx;content:'';"></div>
			      <uni-forms-item prop="firstSignPerson" style="margin-bottom:12rpx;">
					  <span slot="label" class="custom_label">签署人</span>
					  <div class="headerTwo_content">
						<uni-data-select v-model="option.id" :localdata="staffList" :clear="false" placeholder="请选择签署人" @change="selectGet(option)" />
						<span @click="delSign(index)" style="margin:0 20rpx;"><i class="iconfont delIcon">&#xe8c1;</i></span>
					  </div>
			          <!-- 删除 -->
			      </uni-forms-item>
			      <uni-forms-item prop="ecSealCompanyId" style="margin-bottom:12rpx;">
					  <span slot="label" class="custom_label">盖章方式</span>
					  <uni-data-select v-model="option.ecSealCompanyId" class="headerTwo_content" :localdata="sealCompanyList" :clear="false"  placeholder="请选择盖章方式" @change="handlerEcSealCompany(option)" />
			    </uni-forms-item>
			    <!-- 普通签字方式 -->
			        <uni-forms-item prop="isCompanySign" v-if="form.contractSignFileType==1" style="margin-bottom:12rpx;">
						<span slot="label" class="custom_label">签字方式</span>
						<uni-data-select v-model="option.companySignType" class="headerTwo_content" :localdata="signTypeList" :clear="false"  placeholder="无需盖签字章" :disabled="option.companySignType==null" />
			        </uni-forms-item>
			          <!-- 在线模板签字方式 -->
			          <uni-forms-item prop="isCompanySign" v-if="form.contractSignFileType==0" style="margin-bottom:12rpx;">
						  <span slot="label" class="custom_label">签字方式</span>
						  <uni-data-select v-model="option.companySignType" class="headerTwo_content" :localdata="signTypeList" :clear="false" :placeholder="contractTemplatePlaceholder" :disabled="option.companySignType==null" />
			          </uni-forms-item>
			            </div>
			          <div class="text_page" v-if="selectSealData.length>0">签署人有多位时：由经办人发起合同时，根据需求选择其中一位签署即可！</div>
			      </div>
	       </div>
		</uni-forms>
	</view>
</template>
<script>
	import {uniqueArr} from "@/utils";
	import {treeSelectAndStaff} from "@/api/ec/console/org/dept";
	import {selectStaffSealAuthPager} from "@/api/ec/console/seal/staffSeal";
	import {listByCompanyId} from "@/api/ec/console/busiConfig/contractType";
	import {getTemplate, getTemplateByIds} from "@/api/ec/console/busiConfig/template";
	import {listBycontractTypeId} from "@/api/ec/console/busiConfig/contractBusinessType";
	import {listNormalSealCompany, selectSealCompanyListByStaffId} from "@/api/ec/console/seal/sealCompany";
	import {addSignProcess, delSignProcess, getSignProcess, updateSignProcess, updateSignProcessStatus} from "@/api/ec/console/busiConfig/contractSignProcess";
	export default {
		data() {
			return {
				starterArr:[], //默认的合同发起方
				defaultManage:[], //默认选择的流程管理员
				// 选中的员工部门
				selectStartData:[],
				sealCompanyList:[],//盖章方式
				staffList:[],//签署人
				contractTemplatePlaceholder:'使用标准合同模板设置的签署要求',
				placeholderData:'上传本地Word文档编辑完善的文件', //合同文件
				// 签字方式 默认-1  无需签名
				signTypeList:[{text:'标准签字章',value:0},{text:'手写签字章',value:1}],
				// 选中的签署人数组
				selectSealData:[{isStarter:0}],
				// 选中的第一个签署人数据
				firstSignPerson: {id:null,label:null},      
			    // 当前操作的行
			    currentRow:{signSet:null,signPerson:null},
				// 添加的模板list
				contractTemplateList:[{
				  ids:Math.random(),
				  name:null,
				  id:null,
				}],      
				deptOptions: [], // 发起方 使用人数据 
				signRequire:[{text:'使用标准合同模板设置的签署要求',value:1}],
				selectFileArr:[{text:'普通合同签署流程',value:1},{text:'标准合同签署流程',value:0}], //流程类型
				processStatus: [{
					text: '启用',
						value: 1
					}, {
						text: '停用',
						value: 0
					}],	
				// 企业印章
				companySignArr: [{
					text: '企业印章',
					value: 0,
					disabled:true,
					}, {
					text: '授权人签字章',
					value: 1
					}],	
				// 个人签名
				personalSignArr: [
					{
					text: '标准签字章',
					value: 1
					},{
					text: '手写签字章',
					value: 0,
					disabled:false,
					},],	
				contractArr: [], //合同类型
                // 查询参数
                queryParams: {
                  pageNum: 1,
                  pageSize: 10,
                  name: null,
                  isSystem: null,
                  status:[],
                },
				form: {status:1,},
				rules: {
					name: {rules:[{
						required: true,
						errorMessage: "请输入模板名称",
						trigger: "blur"
					}, ]},
				},	
			}
		},
		onLoad(option) {
			this.initNormalSealCompany(); // 加载印章
			this.getContractType(); //获取合同类型
			this.initDept();
			// 签署人 员工
			this.initSealStaffList()
			// 编辑
			if(option.id){
				let id = option.id
				this.getProcess(id)
			}else{
				// 新增 
				this.newAdd()
			}
			uni.$on('templateList',(data) => {
				// 在线合同绑定的合同模板
				this.getTemplateId(data)
			})
		},
		methods: {
			// 选择签约发起人
			selectDep(e){
				this.selectStartData = []
				// 定义发起方数据
				e.forEach((item,index)=>{
					this.selectStartData.push({id:item.id,isStarter:1,label:item.label,level:index})
				})
			},
		    // 选中模板 修改业务绑定的模板id
		    getTemplateId(templateInfo){
		      // 校验重复的id
		      let find = this.contractTemplateList.find(contractTemplate=>contractTemplate.id == templateInfo.id);
		      if(!!find){
		        this.$modal.msgError("已绑定,请重新选择");
		        return;
		      }
		      // 根据模板id 查询模板详情
		      getTemplate(templateInfo.id).then(response => {
		        let contractTemplate = this.contractTemplateList.find(contractTemplate=>contractTemplate.id==this.currentRow.contractTemplateId || contractTemplate.ids==this.currentRow.contractTemplateId);
		        contractTemplate.name = response.data.name;
		        contractTemplate.id = templateInfo.id;
		        // 如果是新增 选中模板后 默认的签署要求只有一个 选中当前为默认签署流程
		        if (this.contractTemplateList.length == 1) {
		          // 默认选中第一个签署要求为默认签署要求
		          this.contractTemplateList[0].isUseSignSet = 1;
		          this.form.contractTemplateSetId=templateInfo.id;
		          // 加载选中的签署要求
		          this.initContractTemplateSet(templateInfo.id);
		        } else {
		          // 多个签署模板签署要求 取选中值的签署要求
		          let contractTemplateIsUseSignSet = this.contractTemplateList.find(contractTemplate=>contractTemplate.isUseSignSet==1);
		          // 加载选中的签署要求
		          this.initContractTemplateSet(contractTemplateIsUseSignSet.id);
		        }
		      })
		    },	
			/** 绑定在线模板 */
			openSelectTemplate(row,item){
				this.currentRow.id = row.id;
				this.currentRow.contractTemplateId=!item.id?item.ids:item.id;
				uni.navigateTo({
					url:'./bindTemplate'
				})
			},
			/** 查询企业印章列表 */
			initNormalSealCompany() {
              this.$modal.loading();
			  listNormalSealCompany(this.queryParams).then(response => {
				uni.hideLoading()
				this.sealCompanyList = []
				response.rows.forEach(item => {
					this.sealCompanyList.push({text:item.name,value:item.id})
				})
			  })
			},
			/* 确认提交签署流程 **/
			submitForm(){ 
				this.$refs.form.validate().then((res) => {
			        // 查询并赋值 签署人第一个 姓名
			        let obj = this.staffList.find((item)=>{//这里的selectList就是上面遍历的数据源
			          return item.value === this.firstSignPerson.id;//筛选出匹配数据
			        });
			        if(!!obj){
			          this.firstSignPerson.label = obj.text;
			        }
					this.form.isCompanySign = this.form.isCompanySign.toString();
					this.form.isCompanySignReceive = this.form.isCompanySignReceive.toString();
			        this.selectSealData.push({id: this.firstSignPerson.id, isStarter: 0, label: this.firstSignPerson.label,index:0,companySignType:this.firstSignPerson.companySignType,ecSealCompanyId:this.firstSignPerson.ecSealCompanyId});
			        // 排序 第一个放前面 
			        this.selectSealData = this.selectSealData.sort((a, b)=>a.index-b.index);
			        // 发起人的数据 + 签署人数据
			        this.form.contractSignProcessUseList = this.selectStartData.concat(this.unique(this.selectSealData));
			        if(!!this.form.manageIds){
			          this.form.manageIds = this.defaultManage
			        }
			        // 保存当前个人签署要求 
			        if(this.form.isPersonSign!="-1"){
			          this.form.isPersonSign= this.form.isPersonSign.join(",");
			        }
			        // 在线模板类型 保存当前选中的模板  
			        if(this.form.contractSignFileType==0){
			          this.form.contractTemplateIds= this.contractTemplateList.map(contractTemplate=>contractTemplate.id).join(",");
			        }
					if(this.form.isCompanySign == '0,1'){
						this.form.isCompanySign = 1
					}
					if(this.form.isCompanySignReceive == '0,1'){
						this.form.isCompanySignReceive = 1
					}
			      if (this.form.id != null) {
			          updateSignProcess(this.form).then(response => {
			            this.$modal.msgSuccess("修改成功");
						uni.$emit('refreshData');
						setTimeout(() => {
							this.cancelSet()
						}, 300)
			          });
			        } else {
			          addSignProcess(this.form).then(response => {
			            this.$modal.msgSuccess("新增成功");
						uni.$emit('refreshData');
						setTimeout(() => {
							this.cancelSet()
						}, 300)
			          });
			        }
			    });	        
			},
			// 授权人签字章设置 同步修改 签署人签署方式
			handlerCompanySignType(e){
			if(e.detail.value.length > 1){
				this.selectSealData.forEach(e=>{
				  e.companySignType=1;
				})
				this.firstSignPerson.companySignType=0;
			}else if(e.detail.value.length == 1){
				//无需盖签字章
				this.selectSealData.forEach(e=>{
				  e.companySignType=null;
				})
				this.firstSignPerson.companySignType=null;
			}
			},
			// 更改合同类型
			selectContractType(value) {
			  this.businessArr = [];
			  // 业务分类
			  listBycontractTypeId(value).then((response) => {
			    this.businessArr = response.data;
			    this.form.contractBusinessTypeId = null;
			  });
			},    
			unique(arr) {
			  const res = new Map();
			  return arr.filter((arr) => !res.has(arr.id) && res.set(arr.id, 1));
			},
		    // 切换签署人下拉框
		    selectGet(option){
		     this.$nextTick(()=>{
		      // 清空选择的印章id
		      option.ecSealCompanyId = null;
		      let obj = this.staffList.find(item=>{ //这里的selectList就是上面遍历的数据源
		         return item.value === option.id; //筛选出匹配数据
		      });
		      // 查找数据 保存name 
		      this.selectSealData.forEach(userData=>{
		        if(userData.id == obj.id){
		          userData.label = obj.name;
		        }
		      });
		      // 根据当前选择的签署人员工id 查询可用的印章
		      selectSealCompanyListByStaffId(option.id).then(response=>{
		        // 根据当前的用户id 重新赋值可用的印章下拉框
		        this.sealCompanyList = []
				response.data.forEach(item => {
					this.sealCompanyList.push({text:item.name,value:item.id})
				})
		      })
			 })
		    },	
		    // 切换第一个签署人下拉框
		    selectFirstGet(staffId){
		      // 清空选择的印章id
		      this.firstSignPerson.ecSealCompanyId=null;
		      let obj = this.staffList.find((item)=>{//这里的selectList就是上面遍历的数据源
		        return item.id === staffId;//筛选出匹配数据
		      });
		      if(!!obj){
		        this.firstSignPerson.label = obj.text;
		      }
		      // 根据当前选择的签署人 查询可用的印章 
		      selectSealCompanyListByStaffId(staffId).then(response=>{
		        // 根据当前的用户id 重新赋值可用的印章下拉框
				this.sealCompanyList = []
				response.data.forEach(item => {
					this.sealCompanyList.push({text:item.name,value:item.id})
				})
		      })
		    },	
		    // 添加在线模板
		    addContractTemplate(){
		      if(this.contractTemplateList.length>9){
		        this.$modal.msgError("最多添加10条");
		        return;
		      }
		      this.contractTemplateList.push({
		        ids:Math.random(),
		        name: null,
		        id:null,
		      });
		    },
		    // 删除模板绑定
		    deleteContractTemplate(data) {
		      this.contractTemplateList.splice(this.contractTemplateList.indexOf(data),1);
		      // 如果删除全部签署要求 则 默认为空
		      if(this.contractTemplateList.length==0){
		        this.contractTemplateList=[{
		          ids:Math.random(),
		          name:null,
		          id:null,
		        }];
		        this.firstSignPerson = {companySignType:null,ecSealCompanyId:null};
		        //  清空签署人
		        this.selectSealData = [];
		        // 签名要求默认
		        this.currentRow={signSet:null,signPerson:null};
		        this.contractTemplatePlaceholder='使用标准合同模板设置的签署要求';
		        return;
		      }
		      // 如果删除当前模板是默认选中要求值 则选中数组0为默认值
		      if (data.isUseSignSet == 1) {
		        this.contractTemplateList[0].isUseSignSet = 1;
		        this.form.contractTemplateSetId=this.contractTemplateList[0].id;
		        // 默认值的签署要求
		        this.initContractTemplateSet(this.contractTemplateList[0].id);
		      }
		    },
			// 新增签署流程
			newAdd(){
				this.reset();//重置表单
				// 添加法人 管理员
				this.form.manageIds=[];
				let legalId = this.$store.getters.staffInfo.legalId;
				let adminId = this.$store.getters.staffInfo.adminId;
				if(legalId!=0 && legalId!=adminId){
				  this.form.manageIds.push(this.$store.getters.staffInfo.legalId);
				}
				this.form.manageIds.push(adminId);
				this.form.manageIds=uniqueArr(this.form.manageIds);
				// 查询当前公司的公章 并选中
				let selectCommonSeal = this.sealCompanyList.filter(seal=>seal.sealTypeId==0);
				if(selectCommonSeal.length!=0){
				  // this.form.ecSealCompanyId = selectCommonSeal[0].id; 
				  this.form.ecSealCompanyId = 185;
				}
				this.form.ecSealCompanyId = 185;
				this.contractTemplatePlaceholder='使用标准合同模板设置的签署要求';
				// 默认第一个签署人为管理员
				this.firstSignPerson.id = adminId;
				
				this.form.staffOrDeptId.push(this.$store.getters.staffInfo.deptIdTop);
				setTimeout(()=>{
					// 流程管理员
					this.defaultManage = this.form.manageIds.toString()
					// 默认的合同发起方
					let arr = []
					arr.push(this.$store.getters.staffInfo.deptIdTop)
					this.starterArr = arr.toString()
				},300)
				this.selectStartData.push({id:this.$store.getters.staffInfo.deptIdTop, level:0,label:this.$store.getters.staffInfo.companyName,isStarter:1})
			},
			// 编辑签章流程信息
		getProcess(id){
			this.reset();
			getSignProcess(id).then(response => {
			this.form = response.data;
			// 签署要求 判断企业接收方设置 
			if (this.form.isCompanySignReceive == '-1'|| this.form.isCompanySignReceive == '0') {
			  this.form.isCompanySignReceive = [0];
			} else {
			  this.form.isCompanySignReceive =[0,1]
			}
			// 签署要求 判断个人签名设置
			if (this.form.isPersonSign == -1) {
			  this.form.isPersonSign = [0, 1];
			} else {
			  this.form.isPersonSign = this.form.isPersonSign.split(",");
			  this.form.isPersonSign = this.form.isPersonSign.map(item =>{
			  	return item-0; //去除双引号
			  })
			}
			// 签署要求 判断企业发起方设置
			if (this.form.isCompanySign == -1 || this.form.isCompanySign == 0) {
			  this.form.isCompanySign = [0];
			} else{
			  this.form.isCompanySign = [0,1]
			}
			if(!!this.form.manageIds){
			  this.form.manageIds = this.form.manageIds.split(",").map(d=>{return parseInt(d)});
			} else {
			  this.form.manageIds=[];
			}
			// 添加法人 管理员 
			let legalId = this.$store.getters.staffInfo.legalId;
			let adminId = this.$store.getters.staffInfo.adminId;
			if(legalId!=0 && legalId!=adminId){
			  this.form.manageIds.push(this.$store.getters.staffInfo.legalId);
			}
			this.form.manageIds.push(adminId);
			this.form.manageIds=uniqueArr(this.form.manageIds);
			// 流程管理员
			setTimeout(()=>{
				this.defaultManage = this.form.manageIds.toString()
			},100)
								
			// 发起人
			this.form.contractSignProcessUseList.filter(userData=>userData.isStarter==1).forEach(userData=>{
			  // 使用人id  使用人部门级别 使用人名称
			  this.selectStartData.push({id:userData.staffOrDeptId, level:userData.level,label:userData.label,isStarter:1})
			  this.form.staffOrDeptId.push(userData.staffOrDeptId);
			  this.$nextTick(()=>{
				  this.starterArr = this.form.staffOrDeptId.toString()
			  })
			})
			
			// 在线模板绑定数据 和签署要求设置 
			if(!!this.form.contractTemplateIds){
			  // 读取模板设置
			  this.initContractTemplate(this.form.contractTemplateIds,this.form.contractTemplateSetId);
			} else {
			  // 当前流程未绑定模板
			  this.contractTemplatePlaceholder='使用标准合同模板设置的签署要求';
			  this.initFirstSignPersonAndSelectSealData();
			}
			})	
		},
		    // 根据模板id 加载在线模板  // 签署人  // 排除第一个签署人 第一个签署人要放进不能删除的第一个删除框中 fixme 此处需要优化
		    initContractTemplate(contractTemplateIds,contractTemplateSetId){
		      getTemplateByIds(contractTemplateIds.split(",")).then(response=>{
		        this.contractTemplateList = response.data;
		        // 获取模板签署要求设置 isUseSignSet =1 的配置 如果只有一个模板 使用当前模板要求 多个模板没指定的话 使用索引为0的配置
		        if( this.contractTemplateList.length==1){
		          this.setCurrentRowSign(this.contractTemplateList[0]);
		        } else {
		          // 若没默认设置  或 设置了默认值 被删除了
		          let currentContractTemplate = this.contractTemplateList.find(contractTemplate=>contractTemplate.id ==contractTemplateSetId);
		          if (contractTemplateSetId==0 || !currentContractTemplate) {
		            this.setCurrentRowSign(this.contractTemplateList[0]);
		          } else {
		            // 若存在设置的模板id
		            let currentContractTemplate = this.contractTemplateList.find(contractTemplate=>contractTemplate.id ==contractTemplateSetId);
		            this.setCurrentRowSign(currentContractTemplate);
		          }
		        }
		      }).then(()=>{
		        this.$forceUpdate();
		        this.initFirstSignPersonAndSelectSealData();
		      });
		    },	
			// 重新赋值当前签署人盖章方式
			initFirstSignPersonAndSelectSealData(){
			  let signList = this.form.contractSignProcessUseList.filter(userData=>userData.isStarter==0);
			  for (let i = 0; i <signList.length; i++) {
			    let signPerson = signList[i];
			    // 把第一个放进 只读输入框中
			    if (i == 0) {
			      this.firstSignPerson = {id:signPerson.staffOrDeptId, level:-1,label:signPerson.label,isStarter:0,index:0,ecSealCompanyId:signPerson.ecSealCompanyId,companySignType:signPerson.companySignType};
			    } else {
			      this.selectSealData.push({id:signPerson.staffOrDeptId, level:-1,label:signPerson.label,isStarter:0,index:i,ecSealCompanyId:signPerson.ecSealCompanyId,companySignType:signPerson.companySignType})
			    }
			  }
			},
			// 根据设置的模板 设置当前签署要求
			setCurrentRowSign(currentContractTemplate){
			  // this.currentRow.signSet = currentContractTemplate.signSet;
			  if(currentContractTemplate.signSet == 1){
			  	this.currentRow.signSet = [0, 1];
			  }else{
			  	this.currentRow.signSet = [0];
			  }
			  this.initContractTemplateCompanySignType();
			  currentContractTemplate.isUseSignSet = 1;
			  // 接收方个人签署设置 默认为空则不限制
			  if (currentContractTemplate.signPerson == null) {
			    this.currentRow.signPerson =[0, 1];
			  } else {
			    this.currentRow.signPerson = currentContractTemplate.signPerson.split(",");
			  }
			},    
		    // 初始 签署人 员工部门
		    initSealStaffList() {
		      selectStaffSealAuthPager().then((response) => {
				let arr = []
				response.rows.forEach(item => {
					arr.push({text:item.name + '（' + item.staffPost+'）',value:item.id})
					this.staffList = arr
				})
		      });
		    },
			handlerEcSealCompany(row){
			      //判空
			      if(!row.id){
			        return;
			      }
			      selectSealCompanyListByStaffId(row.id).then(response => {
			        // 根据当前的用户id 重新赋值可用的印章下拉框
					let arr = []
					response.data.forEach(item => {
						arr.push({text:item.name,value:item.id})
						this.sealCompanyList = arr
					})
			      })
			},
			// 初始化在线模板签署要求
			initContractTemplateCompanySignType(){
			  // 判断如果存在企业签名设置
			  if (this.currentRow.signSet.length == 2) {
			    this.firstSignPerson.companySignType = 1;
			    this.selectSealData.forEach(e => e.companySignType = 1);
			  } else {
			    this.selectSealData.forEach(e => e.companySignType = null);
			    this.firstSignPerson.companySignType = null;
			    this.contractTemplatePlaceholder = "无需签名";
			  }
			},     
		   // 加载当前选中的模板要求
		    initContractTemplateSet(ContractTemplateId){
		      getTemplate(ContractTemplateId).then(response=>{
		        // 所有的默认选中 清空
		        this.contractTemplateList.forEach(data=>{
		          if(data.id == ContractTemplateId){
		            data.isUseSignSet = 1;
		          } else {
		            data.isUseSignSet = 0;
		          }
		        });
		        let currentContractTemplate= response.data;
		        // 企业签名要求 
				if(currentContractTemplate.signSet == 1){
					this.currentRow.signSet = [0, 1];
				}else{
					this.currentRow.signSet = [0];
				}
		        // 判断如果存在企业签名设置
		        this.initContractTemplateCompanySignType();
		        currentContractTemplate.isUseSignSet = 1;
		        // 接收方个人签署设置 默认为空则不限制
		        if (currentContractTemplate.signPerson == null) {
		          this.currentRow.signPerson = [0, 1];
		        } else {
		          this.currentRow.signPerson = currentContractTemplate.signPerson
		        }
		      });
		    },	
			/** 删除 */
			delSign(index){
			   this.selectSealData.splice(index,1)
			 },         
			/** 添加按钮 */
			addcard(){
			  // 签字方式 先判断文件类型 根据文件类型判断签名要求
			  // 在线模板
			  let companySignType=null;
			  if(this.form.contractSignFileType==0 && this.currentRow.signSet!= null && this.currentRow.signSet.length > 1){
			    companySignType = 1;// 标准
			  }
			  // 本地文件
			  if (this.form.contractSignFileType == 1 && this.form.isCompanySign.length > 1) {
			    companySignType = 1;//标准
			  }
			  this.selectSealData.push({
			    companySignType:companySignType,// 默认标准签字章
			    ecSealCompanyId:null,
			    isStarter:0,
			    index:this.selectSealData.length+1,
			  })
			},     
			// 初始 发起方员工部门
			initDept() {
        this.$modal.loading();
			  let data = { isCertify: 1, status: 1 };
			  treeSelectAndStaff(data).then((response) => {
				uni.hideLoading()
				this.deptOptions = response.data;
			  });
			},  
		  /** 合同类型 */
		getContractType() {
		    // 根据当前登录公司 查询合同类型
		    listByCompanyId().then((response) => {
		      let data = response.data;
		  	  var list = []
		  	  for(var i = 0;i<response.data.length;i++){
		  	  	var object = {};
		  	  	object.text = response.data[i].name; // 展示text
		  	  	object.value = response.data[i].id; // 用id查询
		  	  	list.push(object);							
		  	  }
		  	  this.contractArr = list
			  return data[0].id;

		    })
		},
		// 表单重置 
		reset() {
		  this.form = {
		    id: null,
		    name: null,
		    companyId: null,
		    status: 1,
		    contractSignFileType:1, //默认普通合同流程类型 
		    sort: null,
		    isPersonSign:[0,1],
		    staffOrDeptId:[],
		    isCompanySign:[0], //默认无需签名
			isCompanySignReceive:[0],
		  };
		  this.contractTemplateList=[{
		    ids:Math.random(),
		    name:null,
		    id:null,
		  }];
		  this.firstSignPerson = {companySignType:null,ecSealCompanyId:null};
		  this.currentRow={signSet:null,signPerson:null},
		  //个人签名设置 默认值
		  this.selectStartData = [];
		  this.selectSealData = [];
		},  
		cancelSet(){
		  this.reset()
		   uni.navigateBack(1)
		},
		}
	}
</script>

<style scoped lang="scss">
	.headerTwo_content{
	  background: #fff;
	  padding:0px 10rpx 20rpx 0px;
	  display: flex;
	  align-items: center;
	  justify-content: space-between;
	}
	.check_form{
		::v-deep .uni-forms-item.is-direction-top .uni-forms-item__label{
			padding: 0;
		}
		::v-deep .uni-forms-item__label{
			height: 32rpx;
			margin-top: 16rpx;
			color: #777777;
		}
	}
	// 自定义label 表单插槽
	.custom_label{
	 width: 140rpx;
	 text-align: center;
	 padding-top: 14rpx;
	 color: #606266;
	}
	.header_content{
	  background: #fff;
	  padding: 20rpx;
	}
	.header_page{
	  color: $uni-color-primary;
	  background: #e6f6ef;
	  padding: 12rpx 16rpx;
	}
	.text_page{
	  color: $uni-color-primary;
	  background: #e6f6ef;
	  padding: 8rpx 10rpx;
	  margin: 0 10rpx 20rpx 10rpx;
	  font-size: 24rpx;
	}
	.bg-page{
	  background:rgb(240, 240, 240);
	  padding: 14rpx;
	  margin: 20rpx 0 20rpx 10rpx;
	}
	::v-deep .status_sign .checklist-group .checklist-box:first-child{
		width: 200rpx;
	}
	::v-deep .uni-forms-item{margin-bottom: 30rpx;}
	.paddingAll{padding: 20rpx 30rpx 0 10rpx;}
	page{background: $uni-bg-color;}
	// 头部按钮显示
	.header{
		background: #bde8bf;
		height: 80rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 30rpx;
		.change_btn{
			background: #fff;
			margin-right: 0;
			height:50rpx;
			line-height: 50rpx;
			color: $uni-color-primary;
			border:1rpx solid $uni-color-primary;
			font-size: 28rpx;
		}
	}
</style>
